$(function () {
    glfy_init();
})


function glfy_init() {
    //销售单位加载
    $('#glfy_cgdw').combotree({
        loadFilter: function (rows) {
            return convert(rows);
        },
        onSelect: function (record) {
            $.post($.webPath() + '/cwgl/kmys/cyfyys/fylbcombobox', {'gsId': record.id}, function (data) {
                $("#glfy_fylb").combobox({
                    data: data,
                    valueField: 'id',
                    textField: 'fykmjc',
                    multiple: false,
                    panelHeight: '110'
                })
            });
        }
    });


    $.post($.webPath() + '/organ/wls/wlsInuseList', function (jgdata) {
        $("#glfy_sydw").combobox({
            data: jgdata,
            valueField: 'id',
            textField: 'qc',
            multiple: false,
            panelHeight: '200'
        })
    })

    var options = getGlfyGridOption();
    $('#grid_glfy').datagrid(options);
}

function glfy_search() {
    var sydw = $("#glfy_sydw").combobox('getValue');
    var ywlx = $("#toolbar_glfy input[name='queryGlfy.ywlx']").val();
    var fylb = $("#glfy_fylb").combobox('getValue');
    var djh = $("#toolbar_glfy input[name='queryGlfy.djh']").val();
    var jszt = $("#toolbar_glfy input[name='queryGlfy.jszt']").val();
    var cgdw = $("#glfy_cgdw").combotree('getValue');
    var zdsjStart = $('#glfy_zdsj_start').datebox('getValue');
    var zdsjEnd = $('#glfy_zdsj_end').datebox('getValue');


    var options = $.extend(getGlfyGridOption(),
        {pageNumber: 1, queryParams: {djh: djh, ywlx: ywlx, fylbId: fylb, sydwId: sydw, jszt: jszt, cgdwId: cgdw, zdsjStart: zdsjStart, zdsjEnd: zdsjEnd}});
    $('#grid_glfy').datagrid(options);
}

function getGlfyGridOption() {
    var options = {
        url: $.webPath() + "/cggl/glfy/glfylist",
        toolbar: "#toolbar_glfy",
        fitColumns: true,
        singleSelect: true,
        pageNumber: 1,
        pagination: true,
        fit: true,
        rownumbers: true,
        showFooter: true,
        onLoadSuccess: function (target) {
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        loadFilter: function (data) {
            $.each(data.rows, function () {
                if (this.cgdh) {
                    this.djh = this.cgdh;
                } else if (this.xsdh) {
                    this.djh = this.xsdh;
                } else {
                    this.djh = this.gldh;
                }
            })
            return data;
        }
    }

    var columns = {columns: [
        [
            {field: 'djh', title: '单据号', align: 'center', width: 50, formatter: glfyCell},
            {field: 'date_created', title: '日期', align: 'center', width: 30},
            {field: 'gs.jc', title: '公司名称', align: 'center', width: 35},
            {field: 'sydw.jc', title: '收益单位', align: 'center', width: 35},
            {field: 'fylb.fykmjc', title: '费用类别', align: 'center', width: 50},
            {field: 'jfsl', title: '计费数量', align: 'right', width: 50,
                formatter: function (value) {
                    var sl = formatSlForHtml(value, ",");
                    return sl;
                }
            },
            {field: 'dj', title: '单价(元)', align: 'right', width: 50,
                formatter: function (value) {
                    var sl = formatJeForHtml(value, ",");
                    return sl;
                }
            },
            {field: 'je', title: '总金额(元)', align: 'right', width: 50,
                formatter: function (value) {
                    var sl = formatJeForHtml2(value, ",");
                    return sl;
                }
            }
        ]
    ]}
    options = $.extend(options, columns);
    return options;
}